package com.mobi.spring1.Sever.impl;

import com.mobi.spring1.Sever.Sever;
import com.mobi.spring1.dao.AchievementDao;
import com.mobi.spring1.dao.LoginDao;
import com.mobi.spring1.dao.UserDao;
import com.mobi.spring1.pojo.Achievement;
import com.mobi.spring1.pojo.Login;
import com.mobi.spring1.pojo.User;
import com.mobi.spring1.pojo.User_Achievement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class AchievementSever implements Sever {
    @Autowired
    AchievementDao dao;
    @Autowired
    UserDao dao2;

    private Achievement achievement;

    @Override
    public void add(Object o) {
        achievement=(Achievement) o;
        dao.save(achievement);
    }

    @Override
    public void del(Integer id) {
        dao.deleteAchievementsByUid(id);
    }

    @Override
    public void update(Object o) {
        achievement=(Achievement) o;
        dao.save(achievement);
    }

    @Override
    public Object findById(Integer id) {
        return dao.findById(id);
    }

    @Override
    public Object findAll() {
        List<Object> list1 = new ArrayList<Object>();
        List<Achievement> all = dao.findAll();
        for (Achievement a : all) {
            try {


            User_Achievement user_achievement = new User_Achievement();
            int uid = a.getUid();
            User user = dao2.findById(uid).get();
            user_achievement.setUser(user);
            user_achievement.setAchievement(a);
            list1.add(user_achievement);
            }catch (Exception e){
                System.out.println(e);
                continue;
            }
        }
        return list1;
    }
}
